package code.Frame.EPFrame;

import code.Frame.FrameManager;
import code.MYSQL;
import code.Main;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class processFrame {
    public JPanel panel;
    private JLabel title;
    private JTable info;
    private JButton refresh;
    private JButton look;
    private JButton back;
    private JButton ok;

    public boolean fresh;

    public processFrame() {
        refresh.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                refresh_table();
            }
        });
        look.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (info.getSelectedRow() == -1)
                    return;
                String descripton = (String) info.getValueAt(info.getSelectedRow(), 2);
                JOptionPane.showMessageDialog(Main.frame, descripton, "订单内容", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        back.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                fresh = false;
                Main.frame.remove(Main.panel_now);
                Main.panel_now = FrameManager.ep_main;
                Main.frame.setContentPane(Main.panel_now);
                Main.panel_now.revalidate();
            }
        });
        ok.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (info.getSelectedRow() == -1)
                    return;
                int oid = Integer.parseInt((String) info.getValueAt(info.getSelectedRow(), 0));
                try
                {
                    MYSQL.maintain("update orders set status = 1 where OID = " + oid);
                } catch (SQLException ex)
                {
                    throw new RuntimeException(ex);
                }
                refresh_table();
            }
        });
    }

    public void refresh_table()
    {
        String[] colNames = {"OID", "商铺", "描述", "总价", "创建时间", "状态"};
        String[][] data;
        try
        {
            data = MYSQL.query_incomplete_order(Main.uid - 200000000);
        } catch (SQLException ex)
        {
            throw new RuntimeException(ex);
        }
        DefaultTableModel model = new DefaultTableModel(data, colNames);
        info.setModel(model);
    }

    public void start_auto_fresh()
    {
        fresh = true;
        Thread auto_fresh = new Thread(new Runnable() {
            @Override
            public void run()
            {
                while (fresh)
                {
                    try
                    {
                        for (int i=0;i<30 && fresh;i++)
                            Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    refresh_table();
                }
            }
        });
        auto_fresh.start();
    }
}
